/**
 *******************************************************************************
 * @file system_storage.h
 * @author .ISS_AUTO (hudandan@issauto.com)
 * @date 2024-03-23  1. Base version.
 *       2024-11-07  1. Update.
 *
 * @brief This file is interfaces for storage module.
 *
 * @copyright Copyright (c) 2024 ISSAUTO TECH Co., Ltd. All rights reserved.
 *
 *******************************************************************************
 */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __SYSTEM_STORAGE_H
#define __SYSTEM_STORAGE_H

#ifdef __cplusplus
extern "C" {
#endif

/* Exported Includes ---------------------------------------------------------*/
#include "main.h"
#include "eeprom.h"

/* Exported constants --------------------------------------------------------*/
/// Maximal length
#define MAX_STR_LEN             (4 * 8)

/* Exported type -------------------------------------------------------------*/

/** @addtogroup User Flash Management
  * @{
  */

typedef struct {
    const char name[10];
    uint32_t address;
    void *param;
} ssm_info_typedef_t; //!< storage system information

typedef struct {
    uint32_t str_len;
    uint8_t str[MAX_STR_LEN];
    uint8_t check_sum[4];
} ssm_str_param_typedef_t; //!< string parameters storage


typedef enum {
    SSM_FLASH_ID_EE = 0, //!< EEPROM storage Write&Read operations
    #if defined(SP_PROJECT)
    SSM_FLASH_ID_USER,
    #endif

    SSM_FLASH_ID_MAX_NUM,
} ssm_flash_id_typedef_e;

/*!< [SSM_FLASH_ID_EE]: General Parameter */
enum {
    EE_VIRT_INDX_ID =  0,

    #if defined(AK601)

    #endif

    EE_VIRT_INDX_MAX_NUM,
};

#define EE_VIRT_MAX_NUM    EE_VIRT_INDX_MAX_NUM

typedef union {
    uint16_t data[EE_VIRT_MAX_NUM + 1];
    struct {
        uint16_t flash_id;
        #if defined(AK601)

        #endif
        // .
        // .
        // .

    } info;
} ssm_ee_param_typedef_t;


#if defined(SP_PROJECT)
/*!< [SSM_FLASH_ID_USER]: User Infomation */
#pragma pack(4) /* 指定按4字节对齐 */

typedef struct {
    uint32_t id;
    ssm_str_param_typedef_t dname; //!< 设备名称

} ssm_user_info_typedef_t;

#pragma pack() /* 取消指定对齐，恢复缺省对齐 */
#endif

/**
  * @}
  */

/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/** @note Nation系列项目中，EEPROM分配空间为 FLASH_ADDR_USER_START + 2*Pages (size:4K) */
#define ISS_FLASH_USER_STORE        (EEPROM_START_ADDRESS + ISS_PAGE_SIZE * 2)
#if defined(SP_PROJECT)
#define ISS_FLASH_SSM_USER          ISS_FLASH_USER_STORE + (0U * ISS_PAGE_SIZE)
#endif

/* Exported variables --------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/
uint16_t SSM_Info_Read_EE(uint16_t varIdx);
void SSM_Info_Write_EE(uint16_t varIdx, uint16_t new);

void *SSM_Get_Info(uint8_t addr_id);
int SSM_Read_Info(uint8_t addr_id, void *pInfo);
int SSM_Write_Info(uint8_t addr_id, void *pInfo);
void SSM_Write_Info_Default(uint8_t addr_id);
void SSM_Init(void);


#ifdef __cplusplus
}
#endif

#endif /* __SYSTEM_STORAGE_H */

/* END OF FILE ---------------------------------------------------------------*/
